package com.woniuxy.rabbitmq83.configuration;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * description: RabbitMqConfiguration <br>
 * date: 2022/1/12 3:43 下午 <br>
 * author: shesaifei <br>
 * version: 1.0 <br>
 */

/**
 * 配置类编写创建消息队列、交换机的代码
 */

@Configuration
public class RabbitMqConfiguration {
    //1、simple
    //2、work
    //创建消息队列
    @Bean
    public Queue simpleQueue() {
        return new Queue("simple");
    }

    //3、订阅 交换机 绑定 消息队列 1个生产者 1个交换机 2个队列 2个消费之
    //消息队列
    @Bean
    public Queue fanoutQueueOne() {
        return new Queue("one");
    }

    //消息队列
    @Bean
    public Queue fanoutQueueTwo() {
        return new Queue("two");
    }

    //交换机
    @Bean
    public FanoutExchange fanoutExchange() {
        return new FanoutExchange("fanoutExchange");
    }

    //绑定消息队列到交换机上
    @Bean
    public Binding bindingFanoutExchangeAndQueueOne(Queue fanoutQueueOne, FanoutExchange fanoutExchange) {
        //将消息队列绑定到这个交换机上
        return BindingBuilder.bind(fanoutQueueOne).to(fanoutExchange);
    }

    @Bean
    public Binding bindingFanoutExchangeAndQueueTwo(Queue fanoutQueueTwo, FanoutExchange fanoutExchange) {
        //将消息队列绑定到这个交换机上
        return BindingBuilder.bind(fanoutQueueTwo).to(fanoutExchange);
    }
    //4、路由 交换机：direct 路由key

    @Bean
    public Queue directQueueOne() {
        return new Queue("directOne");
    }

    @Bean
    public Queue directQueueTwo() {
        return new Queue("directTwo");
    }

    @Bean
    public DirectExchange directExchange() {
        return new DirectExchange("directExchange");
    }

    @Bean
    public Binding bindingDirectExchangeAndQueueOne(Queue directQueueOne, DirectExchange directExchange) {
        //with(路由)
        return BindingBuilder.bind(directQueueOne).to(directExchange).with("chengdu");
    }

    @Bean
    public Binding bindingDirectExchangeAndQueueTwo(Queue directQueueTwo, DirectExchange directExchange) {
        //with(路由)
        return BindingBuilder.bind(directQueueTwo).to(directExchange).with("chongqing");
    }

    //5、主题 topic 模糊匹配
    @Bean
    public Queue topicQueueOne() {
        return new Queue("topic_queue_one");
    }

    @Bean
    public Queue topicQueueTwo() {
        return new Queue("topic_queue_two");
    }

    @Bean
    public TopicExchange topicExchange() {
        return new TopicExchange("topicExchange");
    }

    @Bean
    public Binding bindingTopicExchangeAndQueueOne(Queue topicQueueOne, TopicExchange topicExchange) {
        return BindingBuilder.bind(topicQueueOne).to(topicExchange).with("#.news");
    }

    @Bean
    public Binding bindingTopicExchangeAndQueueTwo(Queue topicQueueTwo, TopicExchange topicExchange) {
        return BindingBuilder.bind(topicQueueTwo).to(topicExchange).with("chengdu.#");
    }
}
